﻿using System.Collections.Generic;
using MSP.Data.Domain;
using MSP.Data.NHibernate;
using MSP.Data.Repositories;
using MSP.Business.Logic;
using MSP.Data.Domain.Enums;

namespace MSP.Business.ProductManagement
{
    public class ProductCategoryManager
    {
        public int SaveProductCategory(ProductCategory productCategory)
        {
            int productCategoryId = 0;

            if (productCategory != null)
            {
                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var productCategoryRepository = new ProductCategoryRepository(unitOfWork.Session);
                    productCategoryRepository.Save(productCategory);
                    unitOfWork.Commit();
                }

                productCategoryId = productCategory.Id;
            }

            return productCategoryId;
        }

        public int SaveOrUpdate(ProductCategory productCategory)
        {
            int productCategoryId = 0;

            if (productCategory != null)
            {
                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var productCategoryRepository = new ProductCategoryRepository(unitOfWork.Session);
                    productCategoryRepository.SaveOrUpdateCopy(productCategory);
                    unitOfWork.Commit();
                }

                productCategoryId = productCategory.Id;
            }

            return productCategoryId;
        }

        public bool UpdateProductCategory(ProductCategory productCategory)
        {
            bool result = false;

            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var productCategoryRepository = new ProductCategoryRepository(unitOfWork.Session);
                result = productCategoryRepository.Update(productCategory);
                unitOfWork.Commit();
            }

            return result;
        }

        public bool DeleteProductCategory(ProductCategory productCategory)
        {
            bool result = false;
            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var productCategoryRepository = new ProductCategoryRepository(unitOfWork.Session);
                result = productCategoryRepository.Delete(productCategory);
                unitOfWork.Commit();
            }

            return result;
        }

        public ProductCategory GetProductCategoryById(int Id)
        {
            var productCategoryRepository = new ProductCategoryRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            ProductCategory productCategory = productCategoryRepository.GetProductCategoryById(Id);
            return productCategory;
        }

        public IList<ProductCategory> GetAllProductCategory()
        {
            var productCategoryRepository = new ProductCategoryRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            IList<ProductCategory> productCategorys = productCategoryRepository.GetAllProductCategory();

            return productCategorys;
        }
    }
}
